<?php
/**
 * Created by PhpStorm.
 * User: tjjs
 * Date: 17-5-24
 * Time: 上午11:26
 */

namespace ActionsHttp;


use Data\Consumer;
use Data\Consumer_DAO;

class ConsumerLogin extends BaseActionHttp
{
    public function doAction() {
        parent::doAction();
        // 登录类型
        $login_type = (int)$this->data[LOGIN_TYPE];
        $login_id = $this->data[LOGIN_ID];
        $consumer_name = $this->data[CONSUMER_NAME];
        $img_url = $this->data[IMG_URL];

        // 顾客记录
        $consumerRecord = Consumer_DAO::getRecordLoginId($login_id,$login_type);
        if (!$consumerRecord) {
            // 新顾客
            $consumerRecord = new Consumer(array());
            if ($login_type == 1) {
                $consumerRecord->setZFB_ID($login_id);
            } elseif ($login_type == 2) {
                $consumerRecord->setWX_ID($login_id);
            }
            $consumerRecord->setLOGIN_TYPE($login_type);
            $consumerRecord->setIMG_URL($img_url);
            $consumerRecord->setCONSUMER_NAME($consumer_name);
            Consumer_DAO::insert($consumerRecord);
            $consumerRecord = Consumer_DAO::getRecordLoginId($login_id,$login_type);
        }

        $consumerRecord->setIMG_URL($img_url);
        $consumerRecord->setCONSUMER_NAME($consumer_name);
        Consumer_DAO::update($consumerRecord);
        // 老顾客
        // 存储会话
        $_SESSION[CONSUMER_ID] = $consumerRecord->getCONSUMER_ID();
        $_SESSION[SESSION_LOGIN_TIME] = time();
        // 返回数据
        $this->ResponseDataToCurrentClient();
    }
}